define(
["render/BufferMaker", "render/basic/BasicRenderBatch", "framework/graphic/WebGL", "framework/asset/AssetSyst"],
function(BufferMaker, BasicRenderBatch, WebGL, AssetSyst){
    return {
        getName: function() {
            return "COMP - CBasicRender"
        },

        create: function() {

            var _self = {
                init: function(systemMgr, posX, posY, maxX, maxY, imgUrl) {
                	//Create
                	var data = _createVisualData(posX, posY, maxX, maxY, imgUrl);
                	_flushVisualData(systemMgr, data.pos, data.indices, data.tex);

                    //Return me
                    return _self;
                },

                destroy: function() {
                    throw "CBasicRender - destroy - not yet implemented"
                },

                getClass: function() {
                    return _CLASS;
                }
            };

            var _posBufMk;
            var _indBufMk;
            var _texBufMk;
            var _norBufMk;
            var _texture;
            var _basicRenderBatch;
            var _CLASS = this;

            function _flushVisualData(systemMgr, pos, ind, tex) {
                //Create buffers
                var gl = WebGL.gl;
                _posBufMk = BufferMaker.create(gl.ARRAY_BUFFER, Float32Array, 3);
                _texBufMk = BufferMaker.create(gl.ARRAY_BUFFER, Float32Array, 3);
                _norBufMk = BufferMaker.create(gl.ARRAY_BUFFER, Float32Array, 3);
                _indBufMk = BufferMaker.create(gl.ELEMENT_ARRAY_BUFFER, Uint16Array, 1);

                //Add data to buffers
                _posBufMk.add(pos);
                _texBufMk.add(tex);
                _indBufMk.add(ind);
                var normals = [];
                for(var i = 0; i < pos.length; i += 3)
                    normals.push(0,1,0);
                _norBufMk.add(normals);

                //Flush buffers
                _posBufMk.flush();
                _texBufMk.flush();
                _indBufMk.flush();
                _norBufMk.flush();

                //Crate render batch
                _basicRenderBatch = BasicRenderBatch.create().init(
                    systemMgr,
                    _posBufMk.getBuffer(),
                    _texBufMk.getBuffer(),
                    _indBufMk.getBuffer(),
                    _norBufMk.getBuffer(),
                    _texture
                );

                //Set polyoffset
                _basicRenderBatch.setPolyOffset(true, 2, 2);
            }

            function _createVisualData(posX, posY, maxX, maxY, imgUrl) {
		        //Build raw dat
		        var pos = [
		            posX, 0, posY,
		            maxX, 0, posY,
		            maxX, 0, maxY,
		            posX, 0, maxY,
		        ];

		        var indices = [
		            0, 2, 1,
		            0, 3, 2
		        ];

		        var tex = [
		            0, 0, 0,
		            0, 0, 1,
		            0, 1, 1,
		            0, 1, 0,
		        ];

		        _texture = AssetSyst.get(imgUrl);

		        return {
		        	pos: pos,
		        	indices: indices,
		        	tex: tex
		        };
            }

            return _self;
        }
    }
})